package com.ecy.quadratic.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ecy.quadratic.R;
import com.ecy.quadratic.beans.ComicClassifyRecommend;
import com.ecy.quadratic.beans.ComicRecommend;
import com.ecy.quadratic.photopicker.PhotoPreview;
import com.ecy.quadratic.reader.utils.ScreenUtils;
import com.ecy.quadratic.ui.activity.ComicDetailActivity;
import com.ecy.quadratic.widget.ScrollGridView;

import java.util.ArrayList;
import java.util.List;

public class ComicRecommendAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<ComicClassifyRecommend> list;
    private int wh;
    private Fragment mFragment;
    private Activity mActivity;

    public ComicRecommendAdapter(Fragment fragment, List<ComicClassifyRecommend> list) {
        super();
        this.mInflater = LayoutInflater.from(fragment.getContext());
        this.mContext = fragment.getContext();
        mFragment = fragment;
        this.wh = (ScreenUtils.getScreenWidth() - ScreenUtils.dpToPxInt(36)) / 3;
        this.list = list;
    }

    public ComicRecommendAdapter(Activity activity, List<ComicClassifyRecommend> list) {
        super();
        this.mInflater = LayoutInflater.from(activity);
        this.mContext = activity;
        mActivity = activity;
        this.wh = (ScreenUtils.getScreenWidth() - ScreenUtils.dpToPxInt(36)) / 3;
        this.list = list;
    }

    public List<ComicClassifyRecommend> getList() {
        return list;
    }

    public void setList(List<ComicClassifyRecommend> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return list == null ? null : list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return list == null ? null : arg0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (list.size() == 0) {
            return null;
        }
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_fragment_comic_recommend, null);
            holder = new ViewHolder();
            holder.subTitleTV = (TextView) convertView.findViewById(R.id.subtitle_tv);
            holder.moreTV = (TextView) convertView.findViewById(R.id.more_tv);
            holder.imagesGV = (ScrollGridView) convertView.findViewById(R.id.recommend_gridview);
            holder.bindAdapter(null);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ComicClassifyRecommend bean = list.get(position);

        if (bean != null) {
            holder.bindAdapter(bean.recommends);
        } else {
            holder.imagesGV.setVisibility(View.GONE);
        }

        if (bean != null) {
            holder.subTitleTV.setText(bean.classifyName);
        }

        return convertView;
    }

    private void initInfoImages(ScrollGridView imageGridView, final CommunityGridViewAdapter adapter, final ArrayList<String> imgspaths) {
        int itemWidth = wh;
        int itemHeight = wh;
        int gridViewHeight = 0;

        int gridViewWidth = 0;
        switch (imgspaths.size()) {
            case 1:
                gridViewWidth = 3 * wh + ScreenUtils.dpToPxInt(5) * 2;
                gridViewHeight = gridViewWidth * 1 / 2;
                imageGridView.setNumColumns(1);
                itemWidth = gridViewWidth;
                itemHeight = gridViewWidth * 1 / 2;
                break;
            case 2:
                gridViewHeight = wh;
                gridViewWidth = 2 * wh + ScreenUtils.dpToPxInt(5);
                imageGridView.setNumColumns(2);
                break;
            case 4:
                gridViewWidth = 2 * wh + ScreenUtils.dpToPxInt(5);
                gridViewHeight = gridViewWidth;
                imageGridView.setNumColumns(2);
                break;
            case 3:
                gridViewWidth = wh * 3 + ScreenUtils.dpToPxInt(5) * 2;
                gridViewHeight = wh;
                imageGridView.setNumColumns(3);
                break;
            case 5:
            case 6:
                gridViewWidth = wh * 3 + ScreenUtils.dpToPxInt(5) * 2;
                gridViewHeight = 2 * wh + ScreenUtils.dpToPxInt(5);
                imageGridView.setNumColumns(3);
                break;
            default:
                gridViewWidth = wh * 3 + ScreenUtils.dpToPxInt(5) * 2;
                gridViewHeight = 2 * wh + ScreenUtils.dpToPxInt(5);
                imageGridView.setNumColumns(3);
                break;

        }

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) imageGridView.getLayoutParams();
        lp.width = gridViewWidth;
        lp.height = gridViewHeight;
        imageGridView.setLayoutParams(lp);

        adapter.updateData(imgspaths, itemWidth, itemHeight);
        if (mFragment != null) {
            imageGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    PhotoPreview.builder()
                            .setShowDeleteButton(false)
                            .setPhotos(imgspaths)
                            .setCurrentItem(arg2)
                            .start(mContext, mFragment);
//					CommunityDetailActivity.startActivity(mContext,adapter.getItem(arg2));
//					Toast.makeText(mContext, "选择了"+adapter.getItem(arg2)+"图片", Toast.LENGTH_LONG).show();
//					Toast.makeText(mContext, "选择了"+(arg2+1)+"图片", Toast.LENGTH_LONG).show();
                }
            });
        } else {
            imageGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    PhotoPreview.builder()
                            .setShowDeleteButton(false)
                            .setPhotos(imgspaths)
                            .setCurrentItem(arg2)
                            .start(mActivity);
//					CommunityDetailActivity.startActivity(mContext,adapter.getItem(arg2));
//					Toast.makeText(mContext, "选择了"+adapter.getItem(arg2)+"图片", Toast.LENGTH_LONG).show();
//					Toast.makeText(mContext, "选择了"+(arg2+1)+"图片", Toast.LENGTH_LONG).show();
                }
            });
        }


    }

    class ViewHolder {
        TextView subTitleTV;
        TextView moreTV;
        ScrollGridView imagesGV;

        RecommendOneThirdAdapter mAdapter;

        public void bindAdapter(List<ComicRecommend> recommends) {
            mAdapter = new RecommendOneThirdAdapter(mContext, recommends, 6);
            imagesGV.setAdapter(mAdapter);
            imagesGV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    ComicDetailActivity.startActivity(mContext, mAdapter.getItem(position));
                }
            });
        }

        private void updateRecommend(List<ComicRecommend> recommends) {
            mAdapter.setComicRecommend(recommends);
        }
    }


}
